package org.javaforever.cookieshop.dao;

import org.javaforever.cookieshop.domain.TypeBean;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import jakarta.transaction.Transactional;
public interface TypeDao extends JpaRepository<TypeBean, Long>{
	@Query("from TypeBean t")
	public List<TypeBean> listAllTypes() throws Exception;

	@Modifying
	@Query("delete from TypeBean t where t.id = :id")
	@Transactional
	public void deleteType(Long id) throws Exception;

	@Modifying
	@Query("update TypeBean t set t.active = false where t.id = :id")
	@Transactional
	public void softDeleteType(Long id) throws Exception;

	@Query("from TypeBean t where t.id = :id")
	public TypeBean findTypeById(Long id) throws Exception;

	@Query("from TypeBean t where t.name = :name")
	public TypeBean findTypeByName(String name) throws Exception;

	@Query("from TypeBean t where t.active = true")
	public List<TypeBean> listActiveTypes() throws Exception;

	@Modifying
	@Query("update TypeBean t set t.active = not t.active where t.id = :id")
	@Transactional
	public void toggleType(Long id) throws Exception;

	@Modifying
	@Query("update TypeBean t set t.active = true where t.id = :id")
	@Transactional
	public void activateType(Long id) throws Exception;

	@Query("from TypeBean t where t.id = :id")
	public TypeBean findTypeFullInfoById(Long id) throws Exception;

	@Query("from TypeBean t where t.name like CONCAT(:shortName, '%')")
	public List<TypeBean> searchTypeIndexedNamesByShortName(String shortName) throws Exception;

	@Query("select count(*) as countNum from TypeBean t where t.active = true")
	public Integer countActiveTypeRecords() throws Exception;

}
